﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VTV.TaskManagementSystem.Entities;

namespace VTV.TaskManagementSystem.DataAccess
{
    public partial class EmployeeTeam : IConvertToLinqEntity<EmployeeTeam>, ISetting<Entities.EmployeeTeam>
    {
        public string ErrorMessage { get; set; }

        public EmployeeTeam Convert(object baseEntity)
        {
            EmployeeTeam result = null;
            try
            {
                if (baseEntity != null)
                {
                    var entity = (Entities.EmployeeTeam)baseEntity;

                    return result = new EmployeeTeam
                    {
                        TeamID = entity.TeamID,
                        EmployeeID = entity.EmployeeID,
                        IsTeamLead = entity.IsTeamLead,
                        Note = String.IsNullOrEmpty(entity.Note) ? string.Empty : entity.Note,
                    };
                }
                else
                {
                    ErrorMessage = Common.Constant.ErrorMessage.SOURCE_IS_NULL;
                }
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message;
            }

            return result;
        }

        public bool AddNew(Entities.EmployeeTeam source)
        {
            try
            {
                var newSource = this.Convert(source);

                using (var data = new DataBaseDataContext())
                {
                    data.EmployeeTeams.InsertOnSubmit(newSource);
                    data.SubmitChanges();
                }
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message;

                return false;
            }

            return true;
        }

        public bool Delete(Entities.EmployeeTeam source)
        {
            try
            {
                //var newSource = this.Convert(source);

                if (source != null)
                {
                    using (var data = new DataBaseDataContext())
                    {
                        var newSource =
                            data.EmployeeTeams.Single(
                                p => p.EmployeeID == source.EmployeeID && p.TeamID == source.TeamID);
                        //data.EmployeeTeams.Attach(newSource);
                        data.EmployeeTeams.DeleteOnSubmit(newSource);
                        data.SubmitChanges();

                        return true;
                    }
                }
                else
                {
                    return false;
                }
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message;

                return false;
            }
        }

        public bool Update(Entities.EmployeeTeam source)
        {
            try
            {
                using (var data = new DataBaseDataContext())
                {
                    var employeeTeam = data.EmployeeTeams.FirstOrDefault(p => p.EmployeeID == source.EmployeeID && p.TeamID == source.TeamID);
                    if (employeeTeam != null)
                    {
                        employeeTeam.IsTeamLead = source.IsTeamLead;
                        employeeTeam.Note = String.IsNullOrEmpty(source.Note) ? null : source.Note;

                        data.SubmitChanges();

                        return true;
                    }
                    else
                    {
                        ErrorMessage = Common.Constant.SqlErrorMessage.NOT_EXIST_OBJECT_IN_DB;

                        return false;
                    }
                }
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message;

                return false;
            }
        }

        public IEnumerable<Entities.EmployeeTeamToShow> GetByTeam(int teamID)
        {
            try
            {
                using (var data = new DataBaseDataContext())
                {
                    var result = data.EmployeeTeams.Join(data.Employees,
                        employeeTeams => employeeTeams.EmployeeID,
                        employees => employees.EmployeeID,
                        (employeeTeams, employees) => new { employeeTeams, employees })
                        .Where(p => p.employeeTeams.TeamID == teamID && p.employees.IsActive == true)
                        .OrderBy(p => p.employees.FullName)
                        .Select(p => new EmployeeTeamToShow()
                                         {
                                             TeamID = p.employeeTeams.TeamID,
                                             EmployeeID = p.employeeTeams.EmployeeID,
                                             IsTeamLead = p.employeeTeams.IsTeamLead,
                                             Note = p.employeeTeams.Note,
                                             FullName = p.employees.FullName,
                                             Email = p.employees.Email,
                                             Phone = p.employees.Phone
                                         }).ToList();

                    return result;
                }
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message;

                return null;
            }
        }

        public Entities.EmployeeTeam GetEmployeeTeam(int employeeID, int teamID)
        {
            try
            {
                using (var data = new DataBaseDataContext())
                {
                    var result = (from p in data.EmployeeTeams
                                  where p.TeamID == teamID && p.EmployeeID == employeeID
                                  select new Entities.EmployeeTeam
                                  {
                                      EmployeeID = p.EmployeeID,
                                      TeamID = p.TeamID,
                                      IsTeamLead = p.IsTeamLead,
                                      Note = p.Note
                                  }).FirstOrDefault();

                    return result;
                }
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message;

                return null;
            }
        }
    }
}
